/*******************************************************************************
 * Copyright (c) 2010 Gregory Smith (gsmithfarmer@gmail.com).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Gregory Smith (gsmithfarmer@gmail.com) - initial API and implementation
 ******************************************************************************/
/**
 * 
 */
package com.aslan.sfdc.partner.example;

import com.aslan.sfdc.partner.LoginCredentials;
import com.aslan.sfdc.partner.LoginCredentialsRegistry;
import com.aslan.sfdc.partner.LoginManager;

/**
 * Connect to Salesforce using credentials stored in an external XML file.
 * 
 * For this example, credentials called "javaforce" must be defined in the default javaforce
 * configuration XML file ($HOME/sqlforce.ini). The sqlforce.ini file should be in the form:
 * <pre>
 * 
 &lt;sqlforce&gt;
    &lt;connection 
		name="javaforce"
		type="Production"
		username="gsmithfarmer@gmail.com"
		password="yourpassword"
		token="yoursecurity token"
	/&gt;
&lt;/sqlforce&gt;
 * </pre>
 * <p>
 * Where:</p>
 * <ul>
 * <li>Name = name of the profile</li>
 * <li>type = Production or Sandobx</li>
 * <li>username = Salesforce username</li>
 * <li>password = Salesforce password</li>
 * <li>token = Security token generated by Salesforce</li>
 * </ul>
 * <p>
 * To load your own configuration file or to register credentials another way, see the javadoc for {@link com.aslan.sfdc.partner.LoginCredentialsRegistry}.
 * @author gsmithfarmer@gmail.com
 *
 */
public class ConnectUsingProfile {
	/**
	 * Connect to Salesforce using credentials that stored externally in a configuration file.
	 * 
	 * @return an open Salesforce connection
	 * @throws Exception if the login fails.
	 */
	public LoginManager.Session getConnection() throws Exception {
		
		//
		// By default, the LoginCredentialsRegistory will load the configuration file
		// located at $HOME/sqlforce.ini. On my box, this file contains an entry like:
		/*
		<connection 
		name="javaforce"
		type="Production"
		username="gsmithfarmer@gmail.com"
		password="mypassword"
		token="mySalesforceToken"
		/>
	 	*/

		LoginCredentials credentials = LoginCredentialsRegistry.getInstance().getCredentials("javaforce");
		
		if( null == credentials ) {
			throw new Exception("Failed to find Salesforce credentials called 'javaforce'");
		}

		LoginManager.Session session = (new LoginManager()).login( credentials );
		
		return session;
	}
	
	/**
	 * Call all of the example methods in this class.
	 * 
	 * @param args ignored.
	 */
	public static void main( String[] args )  {
		ConnectUsingProfile driver = new ConnectUsingProfile();
		
		try {
			LoginManager.Session session = driver.getConnection();
			
			System.err.println( "You are connected. Are you an administrator? " + session.isAdministrator());
			
			session.logout();
			
		} catch (Exception e) {
			// Drop here if we failed to login into Salesforce.
			e.printStackTrace();
		}
	}

}
